package model.inventory;

import java.util.Map;
import java.util.logging.Level;

import model.Container;
import model.Playable;
import model.Preference;
import model.contents.Content;
import util.LoggingUtils;

public abstract class InventoryRecord implements Playable {
	private int imagePauseTime;
	private Map<Container, Content> contentMap;
	private InventoryRotator rotator;
	private InventoryTable table;
	private InventoryItem item;

	public InventoryRecord(Map<Container, Content> map) {
		this.contentMap = map;
	}

	public void setImagePauseTime(int pauseTimeInSeconds) {
		this.imagePauseTime = pauseTimeInSeconds;
	}

	public boolean play() {
		this.layoutAreas();
		return true;
	}

	public void setRotator(InventoryRotator rotator) {
		this.rotator = rotator;
	}

	private void layoutAreas() {
		for (Container c : contentMap.keySet()) {
			//System.out.println("Playing content: " + this.contentMap.get(c) +
			// " in " + c);

			Content content = this.contentMap.get(c);
			if (content != null) {
				//System.out.println("container name = "+c.getName());
				if(content.isValidContent())
				{
					c.setContent(content);
				}
				else
				{
					System.out.println("Invalid content "+content.getLocalPath());
				}
			}
			else
			{
				System.out.println("Invalid conent null value");

			}

		}
		showTable();
		rotate();
	}

	private void showTable() {
		try
		{
			if (this.table != null && this.table.getContainer() != null) {
				//System.out.println("############");
				this.table.getContainer().setContent(this.table);
			} else {
				System.out.print("not showing table, ");
				if(this.table == null) {
					System.out.println("table is null");
				} else {
					System.out.println("table container is null");
				}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	private void rotate() {
		if (this.rotator == null) {
			if(!Preference.getInstance().isZeroPauseTime()) {
				try {
					Thread.sleep(imagePauseTime * 1000);

				} catch (InterruptedException e) {
					String classMethod = "model.inventory.InventoryRecord .rotate()";
					LoggingUtils.logStart(classMethod);
					LoggingUtils.log(Level.SEVERE,e.getMessage(),e);
				}
			}
		} else {
			for (int i = 0; i < this.rotator.getNumberRotations(); i++) {
				if (rotator.canRotate()) {
					rotator.rotate();
					if(!Preference.getInstance().isZeroPauseTime()) {
						try {
							Thread.sleep(imagePauseTime * 1000);

						} catch (InterruptedException e) {
							String classMethod = "model.inventory.InventoryRecord .rotate()";
							LoggingUtils.logStart(classMethod);
							LoggingUtils.log(Level.SEVERE,e.getMessage(),e);
						}
					}
				}
			}
		}
	}

	public void setTable(InventoryTable table) {
		this.table = table;
	}


	public InventoryTable getTable() {
		// TODO Auto-generated method stub
		return table;
	}

	public Content getFirstImage() {
		if (rotator != null) {
			return rotator.getContent(0);
		}
		return null;
	}

	public void setItem(InventoryItem item) {
		this.item = item;
	}

	public String getFieldValue(String fieldName) {
		return this.item.get(fieldName);
	}
}